<?php

/* Show all errors. */
@ini_set('display_errors', 0);

/*
 * Disable magic_quotes runtime -- it causes problems with legitimate quotes
 * in our SQL, as well as reading/writing the config.php
 */
@ini_set('magic_quotes_runtime', 0);
//echo dirname(__FILE__) . '/phpBIZStub.class';
require_once(dirname(__FILE__) . '/phpBIZStub.class');
require_once(dirname(__FILE__) . '/InstallStep.class');
//require_once(dirname(dirname(__FILE__)) . '/modules/core/classes/GalleryUtilities.class');
define('INDEX_PHP', basename(__FILE__));
define('BASE_DIR',dirname(dirname(__FILE__)));
/*
 * If gettext isn't enabled, subvert the _() text translation function
 * and just pass the string on through in English
 */
if (!function_exists('_')) {
    function _($s) {
	return $s;
    }
}
/* Our install steps, in order */
$stepOrder[] = 'Welcome';
$stepOrder[] = 'Authenticate';
$stepOrder[] = 'SystemChecks';  
$stepOrder[] = 'Database';  
$stepOrder[] = 'AdminUser';
$stepOrder[] = 'CreateConfigFile';
$stepOrder[] = 'Finished';

foreach ($stepOrder as $stepName) {
    require("steps/$stepName.class");
}

if (!ini_get('session.auto_start')) {
    session_start();
}

if (!isset($_SESSION['install_path'])) {
    $_SESSION['install_path'] = __FILE__;
} else if ($_SESSION['install_path'] != __FILE__) {
    /* Security error!  This session is not valid for this copy of the installer. Start over. */
    session_unset();
    $_SESSION['install_path'] = __FILE__;
}

/*
 * If register_globals is on then a global $phpBIZStub may have already been created.
 * Clear it here and initialize ourselves.
 */
unset($phpBIZStub);

if (!isset($_GET['startOver']) && !empty($_SESSION['install_steps'])) {
    $steps = unserialize($_SESSION['install_steps']);
    if (isset($_SESSION['phpBIZStub'])) {
	$phpBIZStub = unserialize($_SESSION['phpBIZStub']);
    }
}

/* If we don't have our steps in our session, initialize them now. */
if (empty($steps) || !is_array($steps)) {
    $steps = array();
    for ($i = 0; $i < count($stepOrder); $i++) {
	$className = $stepOrder[$i];
	$step = new $className();
	if ($step->isRelevant()) {
	    $step->setIsLastStep(false);
	    $step->setStepNumber($i);
	    $step->setInError(false);
	    $step->setComplete(false);
	    $steps[] = $step;
	}
    }

    /* Don't do this in the loop, since not all steps are relevant */
    $steps[count($steps)-1]->setIsLastStep(true);
}

$stepNumber = isset($_GET['step']) ? (int)$_GET['step'] : 0;

/* Make sure all steps up to the current one are ok 保证按步骤进行 */  
for ($i = 0; $i < $stepNumber; $i++) {
    if (!$steps[$i]->isComplete() && !$steps[$i]->isOptional()) {
	$stepNumber = $i;
	break;
    }
}
$currentStep =& $steps[$stepNumber];

if (!empty($_GET['doOver'])) {
    $currentStep->setComplete(false);
}

/* If the current step is incomplete, the rest of the steps can't be complete either */
if (!$currentStep->isComplete()) {
    for ($i = $stepNumber+1; $i < count($steps); $i++) {
	$steps[$i]->setComplete(false);
	$steps[$i]->setInError(false);   /* it is a way of communication*/
    }
}

if ($currentStep->processRequest()) {
    /* Load up template data from the current step */
    $templateData = array();

    /* Round percentage to the nearest 4 */
    $templateData['errors'] = array();
    $currentStep->loadTemplateData($templateData);
    $stepsComplete = max($stepNumber - ($currentStep->isComplete() ? 0 : 1), 0);
    $templateData['percentComplete'] = (int)((100 * ($stepsComplete / (count($steps)-1))) / 4) * 4;

    /* Fetch our page into a variable */
    ob_start();
    include(dirname(__FILE__) . '/templates/MainPage.html');
    $html = ob_get_contents();
    ob_end_clean();

    /* Add session ids if we don't have cookies */
    $html = addSessionIdToUrls($html);
    print $html;
}

/**
 * Add the session id to our url, if necessary
 */
function addSessionIdToUrls($html) {
    /*
     * SID is empty if we have a session cookie.
     * If session.use_trans_sid is on then it will add the session id.
     */
    $sid = SID;
    if (!empty($sid) && !ini_get('session.use_trans_sid')) {
	$html = preg_replace('/href="(.*\?.*)"/', 'href="$1&amp;' . $sid . '"', $html);
    }
    return $html;
}

/*
 * We don't store the steps in the session in raw form because that
 * will break in environments where session.auto_start is on since
 * it will try to instantiate the classes before they've been defined
 */
$_SESSION['install_steps'] = serialize($steps);
if (isset($phpBIZStub)) {
    $_SESSION['phpBIZStub'] = serialize($phpBIZStub);
}
?>
